package character;

import com.jme3.math.FastMath;
import game.CombatController;
import game.GameState;
import game.StateSwitcher;

public class CharacterStatus {
    protected MyCharacter character;
    protected CharacterStatusType characterStatusType = CharacterStatusType.ALIVE;
    protected int ticksPerTurn, hitPoints, initiative;
    protected boolean firstTurn;

    @Deprecated
    protected CharacterStatus() {}

    public CharacterStatus(MyCharacter character) {
	this.character = character;
	ticksPerTurn = character.getCharacterSettings().getAliveTicksPerTurn();
    }

    public int getTicksPerTurn() {
	return ticksPerTurn;
    }
    public void setTicksPerTurn(int ticksPerTurn) {
	this.ticksPerTurn = ticksPerTurn;
    }
    public int getHitPoints() {
        return hitPoints;
    }
    public void setHitPoints(int hitPoints) {
        this.hitPoints = hitPoints;
	clampHitPoints();
	updateCharacterStatusType();
    }
    public void takeDamage(int points) {
        hitPoints -= points;
	clampHitPoints();
	updateCharacterStatusType();
    }
    public void heal(int points) {
        if(characterStatusType != CharacterStatusType.DEAD) {
            hitPoints += points;
            clampHitPoints();
            updateCharacterStatusType();
        }
    }
    public void revive(int points) {
        hitPoints += points;
        clampHitPoints();
        updateCharacterStatusType();
    }
    protected void clampHitPoints() {
	hitPoints = Math.round(FastMath.clamp(hitPoints, -10, character.getCharacterStats().getMaxHitPoints()));
    }
    public int getHitPointPercentage() {
        return Math.round((float)hitPoints / character.getCharacterStats().getMaxHitPoints() * 100);
    }

    public CharacterStatusType getCharacterStatusType() {
        return characterStatusType;
    }
    
    protected void updateCharacterStatusType() {
	if(characterStatusType == CharacterStatusType.ALIVE && hitPoints <=0) {
	    characterStatusType = CharacterStatusType.DYING;
	    ticksPerTurn = character.getCharacterSettings().getDyingTicksPerTurn();
	    System.out.println(character+" is dying.");
	} else if(characterStatusType == CharacterStatusType.DYING && hitPoints <=-10) {
	    characterStatusType = CharacterStatusType.DEAD;
	    ticksPerTurn = 0;
	    if(StateSwitcher.getGameState() == GameState.COMBAT)
		character.getControl(CharacterCombatControl.class).abortQueuedActions();
            else if(StateSwitcher.getGameState() == GameState.ROAM)
                character.getControl(CharacterRoamControl.class).abortQueuedActions();
	    System.out.println(character+" is dead.");
            CombatController.characterDied(character);
	} else if(characterStatusType == CharacterStatusType.DYING && hitPoints > 0) {
            characterStatusType = CharacterStatusType.ALIVE;
            ticksPerTurn = character.getCharacterSettings().getAliveTicksPerTurn();
            System.out.println(character+" is alive again!");
        } else if(characterStatusType == CharacterStatusType.DEAD && hitPoints > -10) {
            if(hitPoints <= 0) {
                characterStatusType = CharacterStatusType.DYING;
                ticksPerTurn = character.getCharacterSettings().getDyingTicksPerTurn();
            } else {
                characterStatusType = CharacterStatusType.ALIVE;
                ticksPerTurn = character.getCharacterSettings().getAliveTicksPerTurn();
            }
            System.out.println(character+" has been revived!");
        }
    }
    
    public int getInitiative() {
	return initiative;
    }
    public void setInitiative(int initiative) {
        this.initiative = initiative;
    }
    public boolean isFirstTurn() {
        return firstTurn;
    }
    public void setFirstTurn(boolean firstTurn) {
        this.firstTurn = firstTurn;
    }
    public void calculateInitiative() {
	initiative = FastMath.rand.nextInt(21);
    }
}
